<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 社群主管理
 */
namespace app\allwin\model;
use app\common\facade\Inform;
use think\Model;

class AllwinUserAgent extends Model{
    
    protected $pk     = 'id';
 
    //和主用户表绑定关系
    public function user(){
        return $this->hasOne('app\common\model\SystemUser','id','uid');
    }

    /**
      * 把已经是社群主的用户排除在外返回用户列表
     */
    public static function selects($condition){
        return model('SystemUser')->where($condition)->whereNotIn('id',self::where(['member_miniapp_id' => $condition['member_miniapp_id']])->column('uid'))
            ->whereNotIn('id',AllwinStore::where(['member_miniapp_id' => $condition['member_miniapp_id']])->column('manage_uid'))->order('id desc')->paginate(10,false);
    }

    /**
     * 添加编辑
     * @param  array $param 数组
     */
    public static function add(int $member_miniapp_id,array $ids){
        $data = [];
        $user = self::where(['member_miniapp_id' => $member_miniapp_id,'uid' => $ids])->field('uid')->select()->toArray();
        $uid = [];
        if(!empty($user)){
            $uid = array_column($user,'uid');
        }
        foreach ($ids as $key => $value) {
            if(!in_array($value,$uid)){
                $data[$key]['member_miniapp_id'] = $member_miniapp_id;
                $data[$key]['uid']               = $value;
                $data[$key]['create_time']       = time();
                $data[$key]['is_type']           = 0;
                $data[$key]['is_pass']           = 1;
                $data[$key]['is_lock']           = 0;
                $data[$key]['pay_state']         = 2;
            }
            //通知申请者到微信
            Inform::sms($value,$member_miniapp_id,['title' =>'业务进展通知','type' => '身份变更','state' => '成功','content' =>'您的身份已变更为社群主']);
        }
        if(empty($data)){
            return;
        }
        return self::insertAll($data);
    }

    /**
     * 读取某个用户是否在某个社群主体系中，并返回当前社群主的ID(计算收益使用)
     * @param integer $uid
     * @param integer $miniapp_id
     * @return void
     */
    public function agentUid(int $uid,int $miniapp_id){
        $level = self::view('allwin_user_agent', 'uid')
                ->view('allwin_user_level', 'level', 'allwin_user_agent.uid=allwin_user_level.parent_id')
                ->where(['allwin_user_level.user_id' => $uid,'allwin_user_agent.member_miniapp_id' => $miniapp_id])
                ->order('allwin_user_level.id asc')->find();
        return empty($level) ? 0 : $level->uid;
    }

    /**
     * @param $data
     * @param $order_no
     * 申请成为社群主
     */
    public static function insertOrder($data,$order_no){
        $order = [
            'member_miniapp_id' => $data['member_miniapp_id'],
            'uid'               => $data['uid'],
            'price'             => $data['price'],
            'order_no'          => $order_no,
            'is_type'           => 1,
            'is_pass'           => 0,
            'is_lock'           => 0,
            'pay_state'         => isset($data['pay_state']) ? $data['pay_state'] : 0,
            'create_time'       => time()
        ];
        self::create($order);
    }



    public static function isLock(int $id){
        $result = self::where(['id' => $id])->field('is_lock')->find();
        $data['is_lock'] = $result['is_lock'] ? 0 : 1;
        return self::where('id',$id)->update($data);
    }

    public static function isPass(int $id,$member_miniapp_id){
        $result = self::where(['id' => $id])->field('is_pass,uid')->find();
        $data['is_pass'] = $result['is_pass'] ? 0 : 1;
        if($data['is_pass'] == 1) {
            //通知申请者到微信
            Inform::sms($result->uid,$member_miniapp_id,['title' =>'业务进展通知','type' => '合伙人申请','content' =>'您的社群主申请已经通过审核','state' => '成功']);
        }
        return self::where('id',$id)->update($data);
    }
}